/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.unsw.bookmarket.service.impl;

import au.edu.unsw.bookmarket.actionform.BookBean;
import au.edu.unsw.bookmarket.actionform.PostAdsBean;
import au.edu.unsw.bookmarket.dao.BookDao;
import au.edu.unsw.bookmarket.jdo.Book;
import au.edu.unsw.bookmarket.service.BookService;
import au.edu.unsw.bookmarket.util.BeanMapping;
import java.util.List;

/**
 *
 * @author gongz
 */
public class BookServiceImpl implements BookService {

    BookDao bookDao;

    public BookDao getBookDao() {
        return bookDao;
    }

    public void setBookDao(BookDao bookDao) {
        this.bookDao = bookDao;
    }

    public BookBean addBook(PostAdsBean postAdsBean) {
        BeanMapping beanMapping = new BeanMapping();
        BookBean book = null;
        try {
            BookBean bookBean = beanMapping.postAdsBean2BookBean(postAdsBean);
            // add if not existence
            if (bookDao.bookExistency(bookBean) == null) {
                bookDao.addBookAsBookBean(bookBean);
            } else {
                return bookDao.bookExistency(bookBean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return book;
    }

    public void addBookDirectly(Book book) {
        try {
            bookDao.addBook(book);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<BookBean> getAllBooks() {
        return bookDao.getAllBooks();
    }

    public boolean removeAllBooks() {
        return bookDao.removeAllBooks();
    }
}
